package com.example.xm.controller;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.example.xm.entity.Userentity;
import com.example.xm.entity.dto.UserDto;
import com.example.xm.exception.SerceiceException;
import com.example.xm.mapper.UserMapper;
import com.example.xm.service.UserService;
import com.example.xm.utils.JWTUtils;
import com.example.xm.web.Result;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.io.IOException;


@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired(required = false)
    UserService userService;
    @Autowired(required = false)
    UserMapper userMapper;
    @PostMapping("/reg")
    public Result reg(@RequestBody Userentity user,HttpSession session){
        String code=(String)  session.getAttribute("regCode");
        if(user.getCode()!=null&&!code.equalsIgnoreCase(user.getCode())){
            throw   new SerceiceException(401,"验证码错误");
        }
        System.out.println("user"+user);
        userService.reg(user);
        return Result.success();
    }
    @PostMapping("/login")
    public Result login(@RequestBody Userentity user,HttpSession session){
      String code=(String)  session.getAttribute("loginCode");
      if(!code.equalsIgnoreCase(user.getCode())){
        throw   new SerceiceException(401,"验证码错误");
      }
//        System.out.println(account+"=========="+password);
        String token = userService.login(user.getName(), user.getPassword());
        return Result.success(token);

    }
    @PostMapping("/update")
    public Result update(@RequestBody Userentity user){
        System.out.println("=========="+user);
      userService.update(user);
        return Result.success();

    }
    @GetMapping("/getUSerInfo")
    public Result getUSerInfo (@RequestHeader("token") String token){
        Claims parse = JWTUtils.parse(token);
        Integer id =(Integer) parse.get("id");
        Userentity user = userMapper.selectById(id);

        return Result.success(user);
    }
    @GetMapping("/list")
    public Result list(UserDto dto){
        PageInfo<Userentity> pageInfo= userService.list(dto);
        return Result.success(pageInfo);
    }
    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable String ids){
        String[] split = ids.split(",");
        int[] intArray = new int[split.length];
        for (int i = 0; i < split.length; i++) {
            intArray[i] = Integer.parseInt(split[i]);
        }
        userService.delete(intArray);
        return Result.success();
    }
    @GetMapping("/captcha")
    public void getCaptcha(HttpServletResponse response, HttpSession session,Integer type) throws IOException {
        // 设置响应头
        response.setContentType("image/jpeg");

        // 生成验证码并写出到 response
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        // 设置颜色
        captcha.setBackground(Color.cyan);
        // 设置干扰线
//        captcha.setLineConfig(new cn.hutool.captcha.Line(2));
        // 设置字体
        captcha.setFont(new Font("Arial", Font.PLAIN, 100));

        String code = captcha.getCode();
        System.out.println(code);
        if(type==1){

        session.setAttribute("loginCode",code);
        }else{
        session.setAttribute("regCode",code);

        }
        String imageBase64 = captcha.getImageBase64();
        // 将验证码写出到 response 中
        captcha.write(response.getOutputStream());
    }
}
